'ui';
let isRunning = false
let wd = null


threads.start(function () {
	// 检测是否打开无障碍权限
	// if (!floaty.checkPermission()) {
	// 	floaty.requestPermission()
	// 	exit()
	// }
	sleep(1000)
	wd = floaty.rawWindow(
		<vertical w="*" h="*">
			<frame layout_weight="1" h="30" layout_gravity="center">
				<img
					w="*"
					h="30"
					src="#ffffff"
					borderWidth="1dp"
					scaleType="fitXY"
					circle="true"
				/>
				<text
					id="start"
					h="30"
					text="开始运行"
					textSize="12sp"
					gravity="center"
					padding="15 0 "
				/>
			</frame>
			<frame h="30" layout_weight="1" layout_gravity="center" marginTop="2">
				<img
					w="*"
					h="30"
					src="#ffffff"
					borderWidth="1dp"
					scaleType="fitXY"
					circle="true"
				/>
				<text
					id="stop"
					h="30"
					text="结束运行"
					textSize="12sp"
					gravity="center"
					padding="15 0"
				/>
			</frame>
		</vertical>
	)
	wd.setPosition(40, 120)
	wd.exitOnClose()
	wd.stop.on('click', function () {
		isRunning = false
		exit()
		toastLog('运行结束')
	})
	wd.start.on('click', function () {
		//程序开始运行之前判断无障碍服务
		if (auto.service == null) {
			toast('请先开启无障碍服务！')
			return
		}


		if (isRunning) {
			toastLog('运行暂停')
			isRunning = false
			ui.run(function () {
				wd.start.setText('开始运行')
			})
			// threads.shutDownAll()
			return
		}

		isRunning = true
		ui.run(function () {
			wd.start.setText('暂停运行')
		})
		threads.start(function () {
			toastLog('运行开始')
			sleep(1000)
			// toastLog('按音量键上,退出脚本。')
			// app.launchApp('微信')
			while (true) {
				if (isRunning) {
					try {
                        main()
                    } catch (error) {
                        
                    }
				}
			}
		})
	})
})

ui.statusBarColor('#000000')
ui.layout(
	<vertical>
		<appbar bg="#000000">
			<toolbar title="茅台抢购" />
		</appbar>
		{/* <Switch
			id="floatx"
			text="悬浮窗权限"
			checked="{{floaty.checkPermission()}}"
			padding="8 8 8 8"
			textSize="15sp"
			gravity="right"
		/> */}
		<Switch
			id="autoService"
			text="无障碍服务"
			checked="{{auto.service != null}}"
			padding="8 8 8 8"
			textSize="15sp"
			gravity="right"
		/>

		<text textSize="12sp" gravity="center" text="(淘宝：保图工作室) 测试（8.22）"/>
	</vertical>
)

// ui.floatx.on('click', function (checked) {
// 	floaty.requestPermission()
// })
ui.autoService.on('check', function (checked) {
	// 用户勾选无障碍服务的选项时，跳转到页面让用户去开启
	if (checked && auto.service == null) {
		app.startActivity({
			action: 'android.settings.ACCESSIBILITY_SETTINGS',
		})
	}
	if (!checked && auto.service != null) {
		auto.service.disableSelf()
	}
})

// 当用户回到本界面时，resume事件会被触发
ui.emitter.on('resume', function () {
	// 此时根据无障碍服务的开启情况，同步开关的状态
	// ui.floatx.checked = floaty.checkPermission()
	ui.autoService.checked = auto.service != null
})
let mBounds = null
function main() {
    if (text('好的').exists()) { 
        text('好的').findOne(1000).click()
    }

	if (!text('茅台预售').exists()) {
		return
	}

	if (
		text('会员尊享 飞天53%vol 500ml贵州茅台酒(带杯)预约销售')
			.visibleToUser()
			.exists()
	) {
		if (text('明日10:00开始预约').visibleToUser().exists()) {
			mBounds = text('明日10:00开始预约').visibleToUser().findOne(1000).bounds()
			toast('预售')
		}else if (text('请在可售时间段内购买').visibleToUser().exists()) {
			mBounds = text('请在可售时间段内购买').visibleToUser().findOne(1000).bounds()
            toast('预售')
		} else {
			click(mBounds.centerX(), mBounds.centerY())
            click(mBounds.centerX(), mBounds.centerY())
            toast('抢购')
		}
		// 上滑
		swiUp()
        // back()
        
	} else if (
		text('飞天53%vol 500ml贵州茅台酒(带杯)预约销售').visibleToUser().exists()
	) {
		let fTv = text('飞天53%vol 500ml贵州茅台酒(带杯)预约销售')
			.visibleToUser()
			.findOne(1000)
		// if (!fTv) { }
		let qTv = fTv.parent().parent().findOne(text('抢购'))
		if (qTv) {
			let qBounds = qTv.bounds()
			click(qBounds.centerX(), qBounds.centerY())
			click(qBounds.centerX(), qBounds.centerY())
        }
        sleep(500)
	}
}

function swiUp() { 
	let x = device.width/2
	let y1 = device.height/5
	let y2 = device.height / 5 * 3
	swipe(x, y1, x, y2, 800)
	sleep(2000)
}

// let x = device.width / 2
// let y1 = device.height / 5
// let y2 = device.height / 5 *3
// swipe(x, y1, x, y2, 1000)

// console.log('sdf');
